<template>
  <div>
    <base-list ref="propertyControl"
               :condition="condition"
               :datas.sync="propertyControlData"
               :pageSearch="pageSearch">
      <el-form :inline="true"
               :model="condition"
               class="search-form"
               slot="search"
               label-width="80px">
        <el-row>
          <el-col :span="6"
                  class="search-width4">
            <el-form-item label="货主单位">
              <auto-search-name v-model="condition['search_EQ_cargoOwnerId']"
                                placeholder="请选择"
                                @selectId="v=>condition['search_EQ_cargoOwnerId']=v"
                                :name="form.cargoOwnerName"></auto-search-name>
            </el-form-item>
          </el-col>
          <el-col :span="6"
                  class="search-width4">
            <el-form-item label="控制单位">
              <auto-search-org v-model="condition['search_EQ_controlOrgName']"
                               placeholder="请选择"
                               @selectName="v=>condition['search_EQ_controlOrgName']=v"
                               :allOrg="true"
                               :name="form.controlOrgName"></auto-search-org>
            </el-form-item>
          </el-col>
          <el-col :span="6"
                  class="search-width4">
            <el-form-item label="控制状态">
              <el-select v-model="condition['search_EQ_controlStatus']"
                         placeholder="请选择"
                         clearable>
                <el-option v-for="[key, val] in _dictControlStatus"
                           :key="key"
                           :label="val"
                           :value="key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button type="primary"
                       icon="el-icon-search"
                       class="ml-80"
                       @click="loadData">查 询</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-row slot="status">
        <el-button type="primary"
                   @click="handleDialogForm">调整控制</el-button>
        <el-button type="primary"
                   @click="stopControl">终止控制</el-button>
        <el-button type="primary"
                   @click="handleViewDialogForm">查看控制记录</el-button>
      </el-row>
      <el-table slot="list"
                :data="propertyControlData"
                border
                stripe
                show-summary
                :summary-method="getSummaries"
                highlight-current-row
                @current-change="handleCurrentChange"
                max-height="500">
        <el-table-column label="序号"
                         type="index"
                         width="50"></el-table-column>
        <el-table-column prop="cargoOwnerName"
                         label="货主单位"
                         min-width="180"></el-table-column>
        <el-table-column prop="controlOrgName"
                         label="控制单位"
                         min-width="200"></el-table-column>
        <el-table-column prop="controlStatus"
                         label="控制状态"
                         min-width="100">
          <template slot-scope="scope">
            {{ scope.row.controlStatus | _filterControlStatus }}
          </template>
        </el-table-column>
        <el-table-column prop="controlType"
                         label="控制类型"
                         min-width="100">
          <template slot-scope="scope">
            {{ scope.row.controlType | _filterControlType }}
          </template>
        </el-table-column>
        <el-table-column prop="controlWay"
                         label="控制方式"
                         min-width="150">
          <template slot-scope="scope">
            {{ scope.row.controlWay | _filterControlWay }}
          </template>
        </el-table-column>
        <el-table-column prop="controlCode"
                         label="控制代码"
                         min-width="100"></el-table-column>
        <el-table-column prop="warehouseName"
                         label="所属仓库"
                         min-width="120"></el-table-column>
        <el-table-column prop="totalReleasedWeight"
                         label="总放货量"
                         min-width="120"></el-table-column>
        <el-table-column prop="totalPledgedWeight"
                         label="总质押量"
                         min-width="120"></el-table-column>
        <el-table-column prop="totalControlWeight"
                         label="实际控制量"
                         min-width="120"></el-table-column>
        <el-table-column prop="totalUseWeight"
                         label="可用量"
                         min-width="120"></el-table-column>
      </el-table>
    </base-list>
    <div style="text-align:center">
      <el-button plain
                 type="info"
                 @click="handleRouterPush({path:'/goods/things',name:'货权管理'})">返回</el-button>
    </div>
    <view-control-record-form ref="viewControlRecordForm"></view-control-record-form>
    <adjust-property-control-form ref="adjustPropertyControlForm"></adjust-property-control-form>
  </div>
</template>

<script>
import {
  getPropertyControlList,
  adjustControl,
  detailPropertyControl,
  stopControl,
  controlRecord
} from '@/api/things/propertyControl'
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoSearchName from '@/components/autocomplete/autoSearchName'
import viewControlRecordForm from './viewControlRecordForm'
import adjustPropertyControlForm from './adjustPropertyControlForm'

export default {
  name: 'propertyControl',
  components: {
    autoSearchOrg,
    autoSearchName,
    viewControlRecordForm,
    adjustPropertyControlForm
  },
  data() {
    return {
      form: this.clearForm(),
      condition: {}, //查询条件
      propertyControlData: [],
      pageSearch: getPropertyControlList,
      id: null,
      item: null,
      recordData: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.loadData()
    })
  },
  methods: {
    clearForm() {
      return {}
    },
    loadData() {
      this.$refs.propertyControl.loadData()
    },
    //返回
    handleRouterPush(val) {
      this.$store.commit('ADD_CACHED_VIEWS', val)
      this.$router.push(val.path)
    },
    //合计
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if(index === 0) {
          sums[index] = '合计'
          return;
        }
        const values = data.map(item => Number(item[column.property]))
        if (
          column.property === 'totalReleasedWeight' ||
          column.property === 'totalPledgedWeight' ||
          column.property === 'totalControlWeight' ||
          column.property === 'totalUseWeight'
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index]
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    //单选
    handleCurrentChange(val) {
      this.id = val.id
      this.item = val
    },
    //调整控制
    handleDialogForm() {
      if (this.item) {
        //判断控制类型是放货还是质押
        if (this.item.controlType === 'FH') {
          this.$refs.adjustPropertyControlForm.zy_disabled = true
          this.$refs.adjustPropertyControlForm.fh_disabled = false
        } else {
          this.$refs.adjustPropertyControlForm.fh_disabled = true
          this.$refs.adjustPropertyControlForm.zy_disabled = false
        }
        detailPropertyControl(this.item.id).then(res => {
          this.$refs.adjustPropertyControlForm.form = JSON.parse(
            JSON.stringify(res.data)
          )
        })
        this.$refs.adjustPropertyControlForm.show = true
        this.$refs.adjustPropertyControlForm.disabled = true
        this.$refs.adjustPropertyControlForm.dialogForm = true
      } else {
        this.$message({ message: '请选中一条数据', type: 'warning' })
      }
    },
    //终止控制
    stopControl() {
      if (this.id) {
        this.$confirm('确认终止所选的控制吗？', '终止控制', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            stopControl(this.id).then(res => {
              this.$message({
                type: 'success',
                message: '终止成功!'
              })
              this.loadData()
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消终止'
            })
          })
      } else {
        this.$message({ message: '请选中一条数据', type: 'warning' })
      }
    },
    //查看控制记录
    handleViewDialogForm() {
      if (this.id) {
        this.$refs.viewControlRecordForm.getId(this.id)
        this.$refs.viewControlRecordForm.dialogForm = true
      } else {
        this.$message({ message: '请选中一条数据', type: 'warning' })
      }
    }
  },
  watch: {
    dialogForm(bool) {
      if (!bool) {
        this.$refs.form.resetFields()
        this.isEdit = false
        this.form = this.clearForm()
        this.$emit('clear')
      }
    }
  }
}
</script>

<style scoped>
</style>
